package cn.akwangl.design.pattern.creational.singleton;

import java.io.*;

/**
 * 单例破坏test.
 * 1. 序列化破坏
 *
 * @author youyongkun
 * @date 2021/11/24 17:15
 */
public class Test2 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        HungrySingleton instance = HungrySingleton.getInstance();

        File singletonFile = new File("singleton_file");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(singletonFile));
        oos.writeObject(instance);
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(singletonFile));
        HungrySingleton newInstance = (HungrySingleton) ois.readObject();

        System.out.println(instance);
        System.out.println(newInstance);
        System.out.println("newInstance == instance:" + (instance == newInstance));
    }
}
